#include<iostream>
using namespace std;

const int N = 1e4 + 5;
int f[N];
bool vis[N];
int cnt, uns;

int find(int i)
{
	if (f[i] == i)
	{
		return i;
	}
	return f[i] = find(f[i]);
}

void un(int a, int b)
{
	int fa = find(a);
	int fb = find(b);
	if (fa != fb)
	{
		f[fa] = fb;
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < N; i++)
	{
		f[i] = i;
	}
	while (n--)
	{
		int k, fa, input;
		cin >> k >> fa;
		vis[fa] = true;
		while (--k)
		{
			cin >> input;
			vis[input] = true;
			un(input, fa);
		}
	}
	for (int i = 1; i < N; i++)
	{
		if (vis[i])
		{
			cnt++;
			if (f[i] == i)
			{
				uns++;
			}
		}
	}
	cout << cnt << " " << uns << endl;
	cin >> n;
	while (n--)
	{
		int a, b;
		cin >> a >> b;
		if (find(a) == find(b))
		{
			cout << "Y" << endl;
		}
		else
		{
			cout << "N" << endl;
		}
	}
	return 0;
}